###################################
##Random Forest Model
###################################
library(randomForest)

#Laod Data
##These data need to be downloaded locally and the path is specific to the user's computer
#load(Vx_Miami.RData)
#load(Vx_Toronto.RData)

#Converting UG_b to Integer 1 (Upgrade) and 0 (Not)
Varix_f$UG_bi<-as.integer(Varix_f$UG_b)-1
C_Varix$UG_bi<-as.integer(C_Varix$UG_b)-1

#Setting Row Names to Sequential Intergers
rownames(Varix_f)<-NULL
rownames(C_Varix)<-NULL

###

#Random Forest Model 
PRF<-randomForest(UG_bi ~ L_Gr + Lat + B_FSH + B_c,data= Varix_f)

RF_test<-predict(PRF, C_Varix)

###

Predicted_Upgrade_Likelihood <- cut(RF_test, br=c(-0.01, 0.416, 0.525,100),labels=c("Low","Eq","Hi"))
table(observed= C_Varix$UG_b,predicted= Predicted_Upgrade_Likelihood)

LR_TT <- table(observed= C_Varix$UG_b,predicted= Predicted_Upgrade_Likelihood)
#LOW Likelihood
LR_TT[2]/(LR_TT[1]+ LR_TT[2])
#Equivocal Likelihood
LR_TT[4]/(LR_TT[3]+ LR_TT[4])
#HIGH Likelihood
LR_TT[6]/(LR_TT[5]+ LR_TT[6])

